//
//  KINGStatuses.swift
//  肉荣微博
//
//  Created by king on 15/8/1.
//  Copyright © 2015年 king. All rights reserved.
//

import UIKit

class KINGStatuses: NSObject{
    /// 微博创建时间
    var created_at: String?
    /// 每个微博都有一个唯一的标记，默认是递增的
    var id: Int = 0
    /// 微博的具体内容
    var text: String?
    /// 微博的来源
    var source: String?
    /// 微博中的图片数组
    var pic_urls: [[String : AnyObject]]? {
        didSet {
            //需要为属性分配只存控件
            pictureUrls = [NSURL]()
            largePicturURLs = [NSURL]()
            for dict in pic_urls! {
                //保存小图URL
                let urlString = dict["thumbnail_pic"] as! String
                pictureUrls?.append(NSURL(string: urlString)!)
                //保存大图URL， 将url中的thumbnail_pic替换为large就可以获得大图
                let largeSting = urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                largePicturURLs?.append(NSURL(string: largeSting)!)
            }
            
            //print(largePicturURLs)
        }
    }
    //存储属性、保存小图URL
    private var pictureUrls:[NSURL]?
    //计算属性，用来获取pictureUrls
    var getPictureUrls:[NSURL]? {
        return retweetedStatus == nil ? pictureUrls : retweetedStatus?.pictureUrls
    }
    //存储属性，保存大图的URL数组
    private var largePicturURLs:[NSURL]?
    //一定要写上可选项，对外提供一个借口
    var getLargePictureURLs:[NSURL]? {
        return retweetedStatus == nil ? largePicturURLs : retweetedStatus?.largePicturURLs
    }
    /// 用户信息模型
    var my_user: KINGUser?
    //有人说这是一个计算属性，为毛就成计算属性了？刀哥求救呀
    var user: [String : AnyObject]? {
        didSet {
            if let dict = user{
                self.my_user = KINGUser(dict: dict)
                //print(self.my_user)
            }
        }
    }
    /// cell行高缓存
    var rowHeight:CGFloat?
    /// 用于中专将微博字典转换成需要的模型
    var retweeted_status:[String:AnyObject]? {
        didSet{
            if let dict = retweeted_status {
                retweetedStatus = KINGStatuses(dict: dict)
            }
        }
    }
    /// 用于给视图使用的微博数据
    var retweetedStatus:KINGStatuses?
    //提供类方法用于从网络中加载数据，控制器不关心，
    class func loadData(since_id: Int, max_id: Int, finish:(dataList:[KINGStatuses]?, error:NSError?)->()) -> () {
        KINGNetworkTools.sharedTools.loadStatuses(since_id, max_id: max_id) { (result, error) -> () in
            if error != nil {
                finish(dataList: nil, error: error)
                return
            }
            if let array = result!["statuses"] as? [[String : AnyObject]] {
                var list:[KINGStatuses] = [KINGStatuses]()
                for dict in array {
                    list.append(KINGStatuses(dict: dict))
                    //print("\(KINGStatuses(dict: dict))")
                }
                /// 这里不能在里面调用，调用多次，而且没有数据的时候，不会做回调
                finish(dataList: list, error: nil)
            } else {
                finish(dataList: nil, error: nil)
            }
        }
    }
    //字典转模型，不需要override 关键字
    init (dict:[String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
//    override func setValue(value: AnyObject?, forKey key: String) {
//        if key == "user" {
//            if let dict = value as? [String:AnyObject] {
//                user = KINGUser(dict: dict)
//                print("----我是来看数据的\(user)")
//            }
//            return
//        }
//        super.setValue(value, forKey: key)
//    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override var description: String {
        let keys = ["created_at", "id", "text", "source", "pic_urls", "user"]
        return "\(dictionaryWithValuesForKeys(keys))"
    }
}
